﻿using System;
using System.IO;
using System.Xml.Linq;

namespace WeiboSharp
{
    public static class Helper
    {
        /// <summary>
        /// 返回文件类型.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <returns></returns>
        public static string GetFileType(this string filePath)
        {
            var fileName = Path.GetFileName(filePath);

            string type;

            switch (Path.GetExtension(fileName).ToLower())
            {
                case ".jpg":
                case ".jpeg":
                    type = "image/jpeg";
                    break;
                case ".gif":
                    type = "image/gif";
                    break;
                case ".png":
                    type = "image/png";
                    break;
                default:
                    throw new ArgumentException(
                        "Can't recognize the extension of the file you're uploading. Please choose either a *.gif, *.jpg, *.jpeg, or *.png file.", filePath);
            }

            return type;
        }

        public static string GetString(this XElement elem, string tagName, string defaultValue = "")
        {
            return
                elem.Element(tagName) == null ||
                elem.Element(tagName).Value == string.Empty
                    ? defaultValue
                    : elem.Element(tagName).Value;
        }

    }
}
